package com.jboost.modules.app.service.strategry.hs;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.constant.CustomerDetailInfo;
import com.jboost.modules.app.constant.CustomerInfo;
import com.jboost.modules.app.dto.HsMatrierDto;
import com.jboost.modules.app.dto.XMUserReqDto;
import com.jboost.modules.app.dto.XmFieldDto;
import com.jboost.modules.app.service.strategry.xm.AbstractXmUser;
import com.jboost.modules.system.entity.HsMaterialNeed;
import com.jboost.modules.system.entity.SysUserDepart;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@Service(value = "HsMaterialQuery")
@Slf4j
public class HsMaterialQuery extends AbstractHsMateria<JsonObject, Boolean> {
    public String getAppId() {
        return "63a2b7b0317aeb00084ff740";
    }

    public String getFormId() {
        return "63b8011fd1811400089d140d";
    }

    public HttpHeaders getJdyHeaders(){
        HttpHeaders jdyHeaders = new HttpHeaders();
        jdyHeaders.set("Authorization","Bearer "+ "w93XMlfhqysv9wWS6Y22f8BfPZ29nvXd");
        return jdyHeaders;
    }

    public static void main(String[] args) throws IOException {
           }

    @Async
    @Scheduled(cron = "0 0/5 * * * ?")
    public BaseResult<Boolean> invokeStrategy() throws Exception {
        log.info("开始批量查询Hs材料数据");
        HashMap<String, Object> param = getParam();
        hsMaterialNeedMapper.delete(Wrappers.lambdaQuery(HsMaterialNeed.class).ne(HsMaterialNeed::getId,0));
        param.put("limit",1000);
        String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
        List<JSONObject> jsonObjects = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
        jsonObjects.forEach(e->{
            List<HsMaterialNeed> list = new ArrayList<>();
            HsMatrierDto.DataBean dataBean = JSONObject.parseObject(e.toString(), HsMatrierDto.DataBean.class);
            List<HsMatrierDto.DataBean.SanjianBean> sanjian = dataBean.getSanjian();
            sanjian.forEach(m->{
                HsMaterialNeed hsMaterialNeed = new HsMaterialNeed();
                //关联编号
                hsMaterialNeed.setQuoteNo(dataBean.get_widget_1672991187317());
                hsMaterialNeed.setMaterialNo(m.getS_material_no());
                hsMaterialNeed.setMaterialName(m.getS_material_name());
                hsMaterialNeed.setMaterialQuality(m.getS_material_quality());
                hsMaterialNeed.setMaterialSpecifications(m.getS_material_specifications());
                hsMaterialNeed.setMaterialRealThickness(m.getS_material_real_thickness()==null?"":m.getS_material_real_thickness()+"");
                hsMaterialNeed.setMaterialType(m.getS_material_type());
                hsMaterialNeed.setMaterialThicknessText(m.getS_material_thickness_text());
                hsMaterialNeed.setMaterialThickness(m.getS_material_thickness()==null?"":m.getS_material_thickness()+"");
                hsMaterialNeed.setMaterialRealThicknessText(m.getS_material_real_thickness_text());
                hsMaterialNeed.setMaterialAllNeed(m.get_widget_1678630788422()+"");
                list.add(hsMaterialNeed);
            });
            List<HsMatrierDto.DataBean.XincaiBean> xincai = dataBean.getXincai();
            xincai.forEach(m->{
                HsMaterialNeed hsMaterialNeed = new HsMaterialNeed();
                //关联编号
                hsMaterialNeed.setQuoteNo(dataBean.get_widget_1672991187317());
                hsMaterialNeed.setMaterialNo(m.getX_material_no());
                hsMaterialNeed.setMaterialName(m.getX_material_name());
                hsMaterialNeed.setMaterialQuality(m.getX_material_quality());
                hsMaterialNeed.setMaterialSpecifications(m.getX_material_specifications());
                hsMaterialNeed.setMaterialRealThickness(m.getX_material_real_thickness());
                hsMaterialNeed.setMaterialType(m.getX_material_type());
                hsMaterialNeed.setMaterialThicknessText(m.getX_material_thickness_text());
                hsMaterialNeed.setMaterialThickness(m.getX_material_thickness_text());
                hsMaterialNeed.setMaterialRealThicknessText(m.getX_material_real_thickness_text());
                hsMaterialNeed.setMaterialAllNeed(m.get_widget_1678630788404()+"");
                list.add(hsMaterialNeed);
            });
            List<HsMatrierDto.DataBean.BancaiBean> bancai = dataBean.getBancai();
            bancai.forEach(m->{
                HsMaterialNeed hsMaterialNeed = new HsMaterialNeed();
                //关联编号
                hsMaterialNeed.setQuoteNo(dataBean.get_widget_1672991187317());
                hsMaterialNeed.setMaterialNo(m.getB_material_no());
                hsMaterialNeed.setMaterialName(m.get_widget_1676017957885());
                hsMaterialNeed.setMaterialQuality(m.getB_material_quality());
                hsMaterialNeed.setMaterialSpecifications(m.getB_material_specifications());
                hsMaterialNeed.setMaterialRealThickness(m.getB_material_real_thickness()==null?"":m.getB_material_real_thickness()+ "");
                hsMaterialNeed.setMaterialType(m.getB_material_type());
                hsMaterialNeed.setMaterialThicknessText(m.getB_material_thickness_text());
                hsMaterialNeed.setMaterialThickness(m.getB_material_thickness()==null?"":m.getB_material_thickness()+"");
                hsMaterialNeed.setMaterialRealThicknessText(m.getB_material_real_thickness_text()==null?"":m.getB_material_real_thickness_text()+"");
                hsMaterialNeed.setMaterialAllNeed(m.get_widget_1678630788419()+"");
                list.add(hsMaterialNeed);
            });
            List<HsMaterialNeed> list1 = list.stream().distinct().collect(Collectors.toList());
            Long index = 1L;
            for (HsMaterialNeed e1:list1){
                e1.setMaterialNo(index++);
            }
            list1.forEach(e2->{
                hsMaterialNeedMapper.insert(e2);
            });
        });



        log.info("开始批量查询Hs材料数据结束");
        return BaseResult.success();
    }
}
